<?php
class mapGoogle extends CI_Controls{

	public $coords;
	public $currentPosition;


	function __construct(){
		parent::__construct();
		$this->coords=array();
		$this->currentPosition=array();
	}

	function drawContent(){
		$map=new div();
		$map->atributos=$this->atributos;
		$map->att('style','width:450px;height:400px');
		//$this->fw->response->page->addCodeScript('googleMapCoords_'.$id.'_'.$instance,ob_get_clean(),false);
		//$this->view_data($map);
		$this->mapScript();
		return $this->libraries().$map ;
		

	}
	public function addCoord($lat,$lng,$name,$image=null,$address=null){

		$coord=array(
				'lat'	=> $lat,
				'lng'	=> $lng,
				'name'	=> $name,
				'image'=>$image
		);
		//?mod=utils/thums&sizeH=alto&sizeW=ancho&dir=
		array_push($this->coords, $coord);

	}

	public function setCurrentPosition($lat,$lng){
		$this->currentPosition=array('lat'=>$lat,'lng'=>$lng,'name'=>'Usted esta aqui');
		$this->addCoord($lat,$lng, 'Usted esta aqui');

	}
	
	private function libraries(){
		 $this->CI->dynamic_assets->add_js("http://maps.google.com/maps/api/js?sensor=true");
	}

	private function mapScript(){

		ob_start();
		?>
			<script>
			$(function(){

				<?php if(count($this->currentPosition)){$centro=$this->currentPosition;} else {$centro=$this->coords[0];}?>
			    var latlng = new google.maps.LatLng(<?php echo $centro['lat']?>,<?php echo $centro['lng']?>);
			    var myOptions = {
			      zoom: 15,
			      zoomControl: true,
			      panControl: false,
			      center: latlng,
			      mapTypeId: google.maps.MapTypeId.ROADMAP
			    };
			    var map = new google.maps.Map(document.getElementById("<?php echo $this->att('id'); ?>"), myOptions);
			    
			    var marker= new Array();
			    var i=0;
			    var latlng = new Array();
			    var infowindow= new Array();
			    <?php $i=0; foreach ($this->coords as $punto){ ?>

			    latlng[<?php echo $i ?>] = new google.maps.LatLng(<?php echo $punto['lat']?>,<?php echo $punto['lng']?>);
			    
			  
			    marker[<?php echo $i ?>] = new google.maps.Marker({
			      position: latlng[<?php echo $i ?>], 
			      map: map,
			      title:"<?php echo $punto['name']; ?>",
			      zIndex: <?php echo $i ?>,
			      <?php if(($i!=0 && count($this->currentPosition)) || (!count($this->currentPosition))){ ?>
				  	icon: new google.maps.MarkerImage('<?php echo $punto['image']?>',new google.maps.Size(48, 48))
				  <?php } ?>
			  	});
			  	
			  	infowindow[<?php echo $i ?>] = new google.maps.InfoWindow(
			      { content: "<?php echo $punto['name']; ?>",
			        size: new google.maps.Size(100,100)
			      });
			  	
			  	
			  	google.maps.event.addListener(marker[<?php echo $i ?>], 'click', function() {
			    	map.panTo(latlng[<?php echo $i ?>]);
			    	infowindow[<?php echo $i ?>].open(map,marker[<?php echo $i ?>]);
			  	});
			  
			  	
			  	<?php ++$i;} ?>
			  	
			    
			});
			</script>
​
		<?php
			
		 $this->CI->dynamic_assets->add_code_js(ob_get_clean());
			
	}
}